package com.itcast.controller;


import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itcast.pojo.*;
import com.itcast.service.Commontservice;
import com.itcast.service.Gcservice;
import com.itcast.util.OutUtil;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.FileWriter;
import java.io.IOException;
import java.util.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ${author}
 * @since 2020-06-19
 */
@Controller
@RequestMapping("/commont")
public class CommontController {
    @Autowired
    private Commontservice commontservice;

    @Autowired
    private Gcservice gcservice;


    @RequestMapping("/toblogPost")
    public String toblogPost() {
        return "redirect:/commont/commontList?page=1";
    }

    @RequestMapping("/commontList")
    public String commList(Model model, Integer page) {
        Page<Commont> pageSize = new Page<>(page, 2);
        IPage<Commont> page1 = commontservice.page(pageSize, null);
        System.out.println(page1);
        model.addAttribute("commList", page1.getRecords());
        model.addAttribute("commpre", page1.getCurrent() - 1);
        model.addAttribute("commnext", page1.getCurrent() + 1);
        System.out.println(page1.getRecords());
        System.out.println(page1.getCurrent() - 1);
        System.out.println(page1.getCurrent() + 1);
        return "qd/blog_post";
    }

    /**
     * 商品评论
     *
     * @param conditionData
     * @param response
     * @param session
     * @return
     */
    @RequestMapping(value = "/commntAdd")
    @ResponseBody
    public String commntAdd(@RequestBody Map<String, String> conditionData, HttpServletResponse response, HttpSession session) {
        String rs = "";
        if (conditionData != null)
            return null;
        if (conditionData.get("userId") != null) {
            int uId = Integer.valueOf(conditionData.get("userId"));
            Integer gid = Integer.valueOf(conditionData.get("sortWay"));
            Commont commont = new Commont();
            commont.setCDate(new Date());
            commont.setContent(conditionData.get("content"));
            commont.setReviewer(conditionData.get("userName"));
            commont.setUid(uId);
            commont.setCount(Integer.valueOf(conditionData.get("counts")));
            commont.setScore(Integer.valueOf(conditionData.get("sortWay")));
            commont.setGid(gid);
            boolean r =commontservice.save(commont);
            if (r){
                Gc gc =new Gc(uId,gid);
                boolean result =gcservice.save(gc);
                if (result){
                    rs  ="ok";
                }else {
                    rs = "error";
                }

            }else{
                rs = "error";

            }

        }else {
            rs = "err";
        }
        return rs;
    }



    //订单评论
    @RequestMapping(value = "/post_comment_qr")
    @ResponseBody
    public String post_comment_qr(@RequestBody Map<String, String> conditionData, HttpServletRequest request,HttpServletResponse response) throws IOException {
        String rs = "";
        Map<String, Object> jsonMap = new HashMap<String, Object>();
        if (conditionData == null)
            return null;
        if (conditionData.get("userId") != null) {
            int uId = Integer.valueOf(conditionData.get("userId"));
            Integer gid = Integer.valueOf(conditionData.get("sortWay"));
            Commont commont = new Commont();
            commont.setCDate(new Date());
            commont.setContent(conditionData.get("content"));
            commont.setReviewer(conditionData.get("userName"));
            commont.setUid(uId);
            commont.setCount(Integer.valueOf(conditionData.get("counts")));
            commont.setScore(Integer.valueOf(conditionData.get("sortWay")));
            commont.setGid(gid);
            boolean r = commontservice.save(commont);
            //更新写入txt
            FileWriter fileWriter = new FileWriter("E:\\SpringBoot\\nxt\\src\\data\\test.txt");
            List<Commont> co = commontservice.selectComment();
            List<Integer> c = new ArrayList<Integer>();
            for (Commont topic : co) {
                c.add(topic.getUid());
                c.add(topic.getGid());
                c.add(topic.getScore());
            }
            Iterator<Integer> iterator = c.iterator();
            //iterator.hasNext()如果存在元素的话返回true
            String line = System.getProperty("line.separator");
            while (iterator.hasNext()) {
                for (int i = 2; i > 0; i--) {
                    if (iterator.hasNext()) {
                        fileWriter.write(String.valueOf(iterator.next()) + ",");
                    }
                }
                if (iterator.hasNext()) {
                    fileWriter.write(String.valueOf(iterator.next()));
                }
                fileWriter.write(line);

            }
            fileWriter.flush();
            fileWriter.close();
            if (r){
                Gc gc =new Gc(uId,gid);
                boolean result =gcservice.save(gc);
                jsonMap.put("msg", "ok");
            }else{
                jsonMap.put("msg", "error");
            }
        }else {
            jsonMap.put("msg", "err");
        }

        String json = JSON.toJSONString(jsonMap);//加入fastjson-1.2.13.jar包
        OutUtil.outcase(json,response);
        return null;
        }

}